package com.enation.app.shop.front.policyInsurance.appController;

import com.enation.app.shop.front.api.inpolicy.entity.InPolicy;
import com.enation.app.shop.front.policyInsurance.adminController.FileEnum.Type1;
import com.enation.app.shop.front.policyInsurance.adminController.FileEnum.Type2;
import com.enation.app.shop.front.policyInsurance.entity.InsuranceFile;
import com.enation.app.shop.front.policyInsurance.entity.img.Pact;
import com.enation.app.shop.front.policyInsurance.service.InsuranceFileMapper;
import com.enation.app.shop.front.policyInsurance.service.InsuranceProtCheckManage;
import com.enation.app.shop.front.policyInsurance.service.InsuranceProtOrdMapper;
import com.enation.framework.action.JsonResult;
import com.enation.framework.util.JsonResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Scope("prototype")
@Controller
@RequestMapping("app/InsuranceProtUpload")
public class InsuranceUploadController {
    @Autowired
    private InsuranceProtOrdMapper inPolicyProtOrdManager;
    @Autowired
    private InsuranceFileMapper insuranceFileMapper;
    @Autowired
    private InsuranceProtCheckManage insuranceProtCheckManage;

    /**
     * 验证电子合同的上传
     * @param inPolicy_id
     * @return
     */
    @ResponseBody
    @RequestMapping("/isUploadPactImg")
    public JsonResult isUploadPactImg(Integer inPolicy_id){
        try {
            InPolicy inPolicy=inPolicyProtOrdManager.getInPolicyDeatil(inPolicy_id);
            if(inPolicy==null){
                JsonResult jsonResult=new JsonResult();
                jsonResult.setData(1);
                jsonResult.setResult(1);
                return jsonResult;
            }else {
                JsonResult jsonResult=new JsonResult();
                jsonResult.setData(0);
                jsonResult.setResult(1);//程序访问成功result是1，异常为0
                return jsonResult;
            }
        }catch (Exception e){
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("操作异常!");
        }
    }

    @ResponseBody
    @RequestMapping("/uploadPact")
    public JsonResult uploadPact(Integer inPolicy_id, String formal_number, List<Pact> pacts){
        try {
            if(pacts==null){
                return JsonResultUtil.getErrorJson("请选择电子版合同照片");
            }
            if(pacts!=null){
                for(Pact pact:pacts){
                    Map map=new HashMap();
                    map.put("formal_number",formal_number);
                    InsuranceFile insuranceFile=new InsuranceFile();
                    insuranceFile.setRelevance_id((long)inPolicy_id);
                    insuranceFile.setType1(Type1.Pact.value());
                    insuranceFile.setType2(Type2.Pact.value());
                    insuranceFile.setType3(pact.getType3());
                    insuranceFile.setUrl(pact.getUrl());
                    insuranceFile.setInstruction("电子版合同照片");
                    insuranceFile.setUpdate_at(new Timestamp(System.currentTimeMillis()));
                    insuranceFile.setCreat_at(new Timestamp(System.currentTimeMillis()));
                    insuranceProtCheckManage.updateInPolicyStatus(inPolicy_id,map);
                    insuranceFileMapper.insert(insuranceFile);
                }
            }
            return JsonResultUtil.getSuccessJson("保存成功!");
        }catch (Exception e){
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("");
        }
    }
}
